﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lbb.Common
{
    /// <summary>
    /// 工具箱
    /// </summary>
    /// <author>Chiron</author>
    public static class Utility
    {
        #region Merger
        /// <summary>
        /// 转为字符串
        /// </summary>
        /// <param name="source">源数据</param>
        /// <param name="se">分隔符</param>
        /// <returns></returns>
        public static string Merger(this Guid[] source, string se = ",")
        {
            return source == null || source.Length < 1 ? null : source.Select(d => d.ToString()).Aggregate((d, e) => d + se + e);
        }
        /// <summary>
        /// 转为字符串
        /// </summary>
        /// <param name="source">源数据</param>
        /// <param name="se">分隔符</param>
        /// <returns></returns>
        public static string Merger(this string[] source, string se = ",")
        {
            return source == null || source.Length < 1 ? null : source.Aggregate((d, e) => d + se + e);
        }
        /// <summary>
        /// 转为字符串
        /// </summary>
        /// <param name="source">源数据</param>
        /// <param name="se">分隔符</param>
        /// <returns></returns>
        public static string Merger(this int[] source, string se = ",")
        {
            return source == null || source.Length < 1 ? null : source.Select(d => d.ToString()).Aggregate((d, e) => d + se + e);
        }
        #endregion Merger

        /// <summary>
        /// 拆分成字符串集
        /// </summary>
        /// <param name="source">源数据</param>
        /// <param name="se">分隔符</param>
        /// <param name="removeEmpty">移除空字符</param>
        /// <returns></returns>
        public static string[] Split(this string source, string se = ",", bool removeEmpty = false)
        {
            return (source ?? "").Split((se ?? ",").ToCharArray(), removeEmpty ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
        }
        /// <summary>
        /// 转为Boolean值
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool? ToBool(this string source)
        {
            bool tv;
            return string.IsNullOrEmpty(source) ? null : bool.TryParse(source, out tv) ? tv : null as bool?;
        }

        /// <summary>
        /// 转为Guid集
        /// </summary>
        /// <param name="source">源数据</param>
        /// <param name="se">分隔符</param>
        /// <param name="removeEmpty">移除空对象</param>
        /// <returns></returns>
        public static Guid[] ToGuids(this string source, string se = ",", bool removeEmpty = false)
        {
            var data = Split(source, se, removeEmpty).Select(d =>
                {
                    Guid id;
                    return Guid.TryParse(d, out id) ? id : Guid.Empty;
                });
            if (removeEmpty) data = data.Where(d => d != Guid.Empty);
            return data.ToArray();
        }
        /// <summary>
        /// 转为Int集
        /// </summary>
        /// <param name="source">源数据</param>
        /// <param name="se">分隔符</param>
        /// <param name="removeEmpty">移除空对象</param>
        /// <returns></returns>
        public static int[] ToInts(this string source, string se = ",", int[] defaultValue = null)
        {
            var data = Split(source, se, true).Select(d =>
            {
                int id;
                return int.TryParse(d, out id) ? id : null as int?;
            });

            var result = data.Where(d => d.HasValue).Select(d => d.Value).ToArray();
            if (result.Length == 0) result = defaultValue ?? result;

            return result;
        }
        /// <summary>
        /// 友好的时间提示
        /// </summary>
        public static string ToFriendlyTime(this DateTime time)
        {
            var now = DateTime.Now;
            var offset = now - time;

            if (time.Year != now.Year && offset.TotalDays > 1) return string.Format(Res.TimeYearsAgo, (int)(offset.TotalDays / 365), time);

            if (time.Month != now.Month && offset.TotalDays > 1) return string.Format(Res.TimeMonthsAgo, (int)(offset.TotalDays / 30), time);

            if (time.Day != now.Day)
            {
                if (time.Month != now.Month || now.Day - time.Day == 1) return string.Format(Res.TimeYesterdayTime, (int)offset.TotalDays, time);
                if (time.Month == now.Month) return string.Format(Res.TimeDaysAgo, (int)offset.TotalDays, time);
            }

            if (offset.TotalHours >= 1)
            {
                if (offset.TotalHours <= 5) return string.Format(Res.TimeHoursBefore, (int)offset.TotalHours, time);
                return string.Format(Res.TimeTodayTime, (int)offset.TotalHours, time);
            }

            if (offset.TotalMinutes >= 1) return string.Format(Res.TimeMinutesBefore, (int)offset.TotalMinutes, time);

            return string.Format(Res.TimeSecondsBefore, (int)offset.TotalSeconds, time);
        }
        /// <summary>
        /// 是否相等(不判断毫秒)
        /// </summary>
        /// <param name="values">要比较的值</param>
        /// <returns></returns>
        public static bool IsEquals(params DateTime?[] values)
        {
            if (values == null || values.Length == 0) return false;
            if (values.Length == 1) return true;
            var v = values[0];
            foreach (var value in values.Skip(1))
            {
                if (v != value)
                {
                    if (!v.HasValue) return false;
                    var v1 = v.Value;
                    var v2 = value.Value;
                    if (v1.Year != v2.Year || v1.Month != v2.Month || v1.Day != v2.Day) return false;
                    if (v1.Hour != v2.Hour || v1.Minute != v2.Minute || v1.Second != v2.Second) return false;
                }
            }
            return true;
        }
    }
}
